<template>
  <el-card>
    <!-- 查询表单 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="mini"
    >
      <el-form-item label="订单号">
        <el-input v-model="formInline.orderNo" placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="收货人">
        <el-input
          v-model="formInline.consignee"
          placeholder="收货人"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="formInline.rderState" placeholder="订单状态">
          <el-option label="派送中" value="派送中"></el-option>
          <el-option label="已受理" value="已受理"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="查看时间">
        <el-date-picker
          v-model="formInline.date"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitbtn">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <template>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="orderNo" label="订单号"> </el-table-column>
        <el-table-column label="下单时间" width="220">
          <template v-slot="row">
            {{ row.orderTime | time }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="consignee" label="收货人" width="120">
        </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址" width="300">
        </el-table-column>
        <el-table-column label="送达时间" width="220">
          <template v-slot="row">
            {{ row.deliveryTime | time }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注" width="120">
        </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额" width="120">
        </el-table-column>
        <el-table-column prop="orderState" label="订单状态" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template v-slot="{ row }">
            <el-button @click="handleClick(row)" type="text" size="small"
              >查看</el-button
            >
            <router-link :to="'/order/bianji?id=' + row.id">
              <el-button type="text" size="small">编辑</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="pageSizes"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog
      title="查看当前行的数据"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <ul>
        <!--    {{ main }} -->
        <li>
          <span>订单号:</span>
          {{ main.orderNo }}
        </li>
        <li>
          <span>下单时间:</span>
          {{ main.orderTime }}
        </li>
        <li>
          <span>联系电话:</span>
          {{ main.phone }}
        </li>
        <li>
          <span>收货人:</span>
          {{ main.consignee }}
        </li>
        <li>
          <span>送货地址:</span>
          {{ main.deliverAddress }}
        </li>
        <li>
          <span>送达时间:</span>
          {{ main.deliveryTime }}
        </li>
        <li>
          <span>备注:</span>
          {{ main.remarks }}
        </li>
        <li>
          <span>订单金额:</span>
          {{ main.orderAmount }}
        </li>
        <li>
          <span>订单状态:</span>
          {{ main.orderState }}
        </li>
      </ul>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false" size="mini"
          >关闭</el-button
        >
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { orderlist } from '../../api/shop'
import moment from 'moment'
import _ from 'lodash'
export default {
  data() {
    return {
      currentPage: 1,
      pageSizes: 5,
      total: 0,
      tableData: [],
      // 查询的数据对象
      formInline: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      },
      dialogVisible: false,
      main: {}
    }
  },
  created() {
    this.orderList()
  },
  methods: {
    // 获取订单列表
    async orderList() {
      const data = await orderlist({
        currentPage: this.currentPage,
        pageSize: this.pageSizes,
        orderNo: this.formInline.orderNo,
        consignee: this.formInline.consignee,
        phone: this.formInline.phone,
        orderState: this.formInline.orderState,
        date: JSON.stringify(this.formInline.date)
      })
      // console.log(data)
      this.tableData = data.data
      this.total = data.total
    },
    // 查看
    handleClick(obj) {
      this.main = _.cloneDeep(obj) // 克隆，防止双向数据绑定直接修改
      this.dialogVisible = true
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSizes = val
      this.orderList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.orderList()
    },
    // 点击查询，调用orderList() 需要把页数设置为1
    submitbtn() {
      this.currentPage = 1
      this.orderList()
    }
  },
  filters: {
    time(str) {
      return moment(str).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 20px;
}
.el-form {
  margin-bottom: 50px;
}
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner {
  width: 300px;
}
li {
  display: flex;
  margin-bottom: 10px;
  align-items: center;
  span {
    width: 100px;
    vertical-align: middle;
  }
}
</style>
